package com.tbit.uqbike.center.config.ThreadPoolConfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.center.constant.GatewayProtocol;
import com.tbit.uqbike.center.pojo.TerBattery;
import com.tbit.uqbike.center.pojo.TerPosition;
import com.tbit.uqbike.center.service.MachineStatusService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 消费MQ消息 同步终端上报（位置，电池，电压）数据 线程体
 * @author Zhang Zhi Liang
 * @date 2024/4/22 10:39
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class MsgRunnable implements Runnable{
    /** MQ消息对象 */
    private String message;

    /** 终端状态同步service */
    private MachineStatusService machineStatusService;

    @Override
    public void run() {
        try {
            /**判断消息类型*/
            JSONObject object = JSON.parseObject(message);
            int msgId = object.getIntValue("msgId");
            switch (msgId) {
                case GatewayProtocol.MQ_MSG_ID_POS_PUSH:
                    TerPosition terPosition = JSON.parseObject(object.getString("data"), TerPosition.class);
                    machineStatusService.updatePosition(terPosition);
                    break;
                case GatewayProtocol.MQ_MSG_ID_BATTERY_PUSH:
                    TerBattery terBattery = JSON.parseObject(object.getString("data"), TerBattery.class);
                    machineStatusService.updateBattery(terBattery);
                    break;
                case GatewayProtocol.MQ_MSG_ID_LITHIUM_BATTERY_PUSH:
                    TerBattery lithium = JSON.parseObject(object.getString("data"), TerBattery.class);
                    //log.info("锂电池信息="+lithium);
                    machineStatusService.updateLithiumBattery(lithium);
                    break;
                default:
                    log.warn("未知消息类型=" + msgId);
            }
        }catch (Exception e){
            log.warn(String.format("%s线程执行异常,消息%s消费失败",Thread.currentThread().getName(),message));
        }
    }
}
